//
//  TCLYBabyGoodsViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/12.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/*****************************************************************************************点击个别商品进入该商品详情的界面******************************************************************************************************/

#import "TCLYBabyGoodsViewController.h"
#import "SDCycleScrollView.h"
#import "Macros.h"
#import "Masonry.h"
#import "TCLYHomeModel.h"
#import "UIImageView+WebCache.h"
#import "TCLYCommentViewController.h"
#import "TCLYDetermineOrderViewController.h"
@interface TCLYBabyGoodsViewController ()<SDCycleScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewLayout;

//顶部放滚动栏的View
@property (weak, nonatomic) IBOutlet UIView *headerView;
//商品的名字
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//商品的简单描述
@property (weak, nonatomic) IBOutlet UILabel *introduceLabel;
//商品价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
//邮费价格
@property (weak, nonatomic) IBOutlet UILabel *postageLabel;
//评价
@property (weak, nonatomic) IBOutlet UILabel *evaluationLabel;
//用户评论的头像
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;
//评论用户的昵称
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
//评论的详细信息
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *lineLabel;

@property (weak, nonatomic) IBOutlet UIView *selectView;

@property (weak, nonatomic) IBOutlet UIButton *selectLeftBtn;
@property (weak, nonatomic) IBOutlet UIButton *selectRightBtn;
//评论的时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
//评论的按钮
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;


@property (nonatomic, strong) SDCycleScrollView * cycleScrollView;
@property (nonatomic, strong) TCLYHomeModel * homeModel;
@property (nonatomic, strong) NSMutableArray * imageArr;
@property (nonatomic, strong) NSMutableArray * goodsIntroduceArr;
@property (nonatomic, strong) NSMutableArray * goodsStandardArr;

@property (nonatomic, strong) UIView * goodsIntroduceView;
@property (nonatomic, strong) UIView * goodsStandardView;
@end

@implementation TCLYBabyGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    
    UIView * titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 0, screenWidth, 20);
    titleView.backgroundColor = statusColor;
    [self.view addSubview:titleView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createGoodsStandardView) name:@"fff" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createGoodsView) name:@"eee" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aaa) name:@"ddd" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadBabyGoodsDetailed) name:@"loadBabyGoodsDetailed" object:nil];
    self.homeModel = [[TCLYHomeModel alloc] init];
    NSLog(@"%@",self.parameterDic);
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.parameterDic[@"goods_id"],@"id", nil];
    NSLog(@"%@",dic);
    [self.homeModel loadBabyDetailedParameter:dic];
    self.bgView.backgroundColor = bgColor;
    [self createView];
    
}

//加载图片 （没有加线程）
-(void)loadBabyGoodsDetailed{
    self.imageArr = [NSMutableArray array];
    self.goodsIntroduceArr = [NSMutableArray array];
    self.goodsStandardArr = [NSMutableArray array];
    //这个是顶部展示商品的滚动栏上的图片
    for (int i = 0; i<self.homeModel.goodsCarouselArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL * url = [NSURL URLWithString:self.homeModel.goodsCarouselArr[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self.imageArr addObject:imageView.image];
            if (self.imageArr.count == self.homeModel.goodsCarouselArr.count) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ddd" object:self];
            }
        }];
    }
    //这个是商品详细介绍的图片
    for (int i = 0; i<self.homeModel.goodsProductArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL * url = [NSURL URLWithString:self.homeModel.goodsProductArr[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self.goodsIntroduceArr addObject:imageView.image];
            if (self.imageArr.count == self.homeModel.goodsProductArr.count) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"fff" object:self];
            }
        }];
    }
    //这个是规格介绍里面的图片
    for (int i = 0; i<self.homeModel.goodsStandardArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL * url = [NSURL URLWithString:self.homeModel.goodsStandardArr[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self.goodsStandardArr addObject:imageView.image];
            if (self.imageArr.count == self.homeModel.goodsStandardArr.count) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ccc" object:self];
            }
        }];
    }
    //以下是包装该页面数据的
    self.nameLabel.text = [NSString stringWithFormat:@"%@%@",self.homeModel.goodsDetailedDic[@"brand"],self.homeModel.goodsDetailedDic[@"goods_name"]];
    self.introduceLabel.text = self.homeModel.goodsDetailedDic[@"standard"];
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",self.homeModel.goodsDetailedDic[@"goods_price"]];
    self.postageLabel.text = [NSString stringWithFormat:@"邮费: %@",self.homeModel.goodsDetailedDic[@"express"]];
    NSLog(@"%@",self.homeModel.goodsCommentNumDic[@"comment_num"]);
    //判断当前商品是否有评论
    if (![self.homeModel.goodsCommentNumDic[@"comment_num"] isEqual:@"0"]) {
        self.evaluationLabel.text = [NSString stringWithFormat:@"%@",self.homeModel.goodsCommentNumDic[@"comment_num"]];
        self.nickNameLabel.text = self.homeModel.goodsCommentArr[0][@"uname"];
        [self.headerImage sd_setImageWithURL:self.homeModel.goodsCommentArr[0][@"upic"] placeholderImage:[UIImage imageNamed:@"LYJia2"]];
        self.contentLabel.text = self.homeModel.goodsCommentArr[0][@"content"];
        self.timeLabel.text = self.homeModel.goodsCommentArr[0][@"time"];
    }else{
        self.evaluationLabel.text = [NSString stringWithFormat:@"%@",self.homeModel.goodsCommentNumDic[@"comment_num"]];
        self.contentLabel.text = @"目前还没有用户对此商品做出评论";
    }
}

//通知加载完图片后要做的事情
-(void)aaa{
    //    NSLog(@"%ld",self.imageArr.count);
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, screenWidth, 125) imagesGroup:self.imageArr];
    self.cycleScrollView.infiniteLoop = YES;
    self.cycleScrollView.delegate = self;
    //        cycleScrollView.titlesGroup = titlesArr;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    self.cycleScrollView.autoScrollTimeInterval = 3.0;
    self.cycleScrollView.dotColor = [UIColor whiteColor];
    [self.headerView addSubview:self.cycleScrollView];
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerView);
        make.right.mas_equalTo(self.headerView);
        make.top.mas_equalTo(self.headerView);
        make.height.mas_equalTo(184);
    }];
}

//详细页面所有的视图
-(void)createView{
    //拖动查看详情左面的细线
    UIImageView * leftLineImageView = [[UIImageView alloc] init];
    leftLineImageView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:leftLineImageView];
    [leftLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.lineLabel);
        make.left.mas_equalTo(self.bgView.mas_left).offset(6);
        make.right.mas_equalTo(self.lineLabel.mas_left).offset(-6);
        make.height.mas_equalTo(1);
    }];
    //拖动查看右面的细线
    UIImageView * rightLineImageView = [[UIImageView alloc] init];
    rightLineImageView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:rightLineImageView];
    [rightLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.lineLabel);
        make.left.mas_equalTo(self.lineLabel.mas_right).offset(6);
        make.right.mas_equalTo(self.bgView.mas_right).offset(-6);
        make.height.mas_equalTo(1);
    }];
    self.headerImage.layer.cornerRadius = 15;
    self.headerImage.layer.masksToBounds = YES;
    [self.commentBtn setTitleColor:grayFont forState:UIControlStateNormal];
    [self.commentBtn setTitle:@"查看所有评论" forState:UIControlStateNormal];
    self.commentBtn.layer.cornerRadius = 5;
    self.commentBtn.layer.borderColor = btnColor.CGColor;
    self.commentBtn.layer.borderWidth = 1;
    [self.selectLeftBtn setTitleColor:btnColor forState:UIControlStateNormal];
    [self.selectRightBtn setTitleColor:btnColor forState:UIControlStateNormal];

    
    self.selectLeftBtn.selected = YES;
    self.selectRightBtn.selected = NO;
    
    UIImageView * leftImageView = [[UIImageView alloc] init];
    leftImageView.backgroundColor = btnColor;
    leftImageView.tag = 81;
    [self.selectView addSubview:leftImageView];
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectView.mas_bottom);
        make.left.mas_equalTo(self.selectLeftBtn);
        make.right.mas_equalTo(self.selectLeftBtn);
        make.height.mas_equalTo(1);
    }];
    UIImageView * rightImageView = [[UIImageView alloc] init];
    rightImageView.backgroundColor = btnColor;
    rightImageView.tag = 82;
    rightImageView.hidden = YES;
    [self.selectView addSubview:rightImageView];
    [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectView.mas_bottom);
        make.left.mas_equalTo(self.selectRightBtn);
        make.right.mas_equalTo(self.selectRightBtn);
        make.height.mas_equalTo(1);
    }];
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(self.bgView.mas_left).offset(12);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    UIButton * submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"立即抢购" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    submitBtn.backgroundColor = btnColor;
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
}
//产品图片的View视图
-(void) createGoodsView{
    self.goodsIntroduceView = [[UIView alloc] init];
    [self.bgView addSubview:self.goodsIntroduceView];
    [self.goodsIntroduceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectView.mas_bottom).offset(12);
        make.left.mas_equalTo(self.bgView);
        make.right.mas_equalTo(self.bgView);
        make.height.mas_equalTo(200*self.goodsIntroduceArr.count);
    }];
    for (int i = 0; i < self.goodsIntroduceArr.count; i++) {
        UIImageView * goodsImageView = [[UIImageView alloc] init];
        goodsImageView.image = self.goodsIntroduceArr[i];
        goodsImageView.tag = 10000+i;
        [self.goodsIntroduceView addSubview:goodsImageView];
        if (i == 0 ) {
            [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.goodsIntroduceView);
                make.left.mas_equalTo(self.goodsIntroduceView);
                make.right.mas_equalTo(self.goodsIntroduceView);
                make.height.mas_equalTo(200);
            }];
        }else{
            [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo([self.goodsIntroduceView viewWithTag:goodsImageView.tag-1].mas_bottom);
                make.left.mas_equalTo(self.goodsIntroduceView);
                make.right.mas_equalTo(self.goodsIntroduceView);
                make.height.mas_equalTo(200);
            }];
        }
    }
    self.scrollViewLayout.constant = 577+200*self.goodsIntroduceArr.count;
}
//显示产品规格的View视图
-(void)createGoodsStandardView{
    self.goodsStandardView = [[UIView alloc] init];
    self.goodsStandardView.hidden = YES;
    [self.bgView addSubview:self.goodsStandardView];
    [self.goodsStandardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectView.mas_bottom).offset(12);
        make.left.mas_equalTo(self.bgView);
        make.right.mas_equalTo(self.bgView);
    }];
    for (int i = 0; i < self.goodsStandardArr.count; i++) {
        UIImageView * goodsImageView = [[UIImageView alloc] init];
        goodsImageView.tag = 10000+i;
        goodsImageView.image = self.goodsStandardArr[i];
        [self.goodsStandardView addSubview:goodsImageView];
        if (i == 0 ) {
            [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.goodsStandardView);
                make.left.mas_equalTo(self.goodsStandardView);
                make.right.mas_equalTo(self.goodsStandardView);
                make.height.mas_equalTo(200);
            }];
        }else{
            [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo([self.goodsStandardView viewWithTag:goodsImageView.tag-1].mas_bottom);
                make.left.mas_equalTo(self.goodsStandardView);
                make.right.mas_equalTo(self.goodsStandardView);
                make.height.mas_equalTo(200);
            }];
        }
    }
    [self.goodsStandardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(200*self.goodsStandardArr.count);
    }];
}

//选择查看产品图片或者是产品规格的按钮事件
-(IBAction)selectBtnAction:(id)sender{
    //找到Tag值  
    UIImageView * left = [self.selectView viewWithTag:81];
    UIImageView * right = [self.selectView viewWithTag:82];
    if ((UIButton *)sender == self.selectLeftBtn) {
        self.selectLeftBtn.selected = YES;
        self.selectRightBtn.selected = NO;
    }else{
        self.selectLeftBtn.selected = NO;
        self.selectRightBtn.selected = YES;
    }
    if (self.selectLeftBtn.selected == YES) {
        self.goodsStandardView.hidden = YES;
        self.goodsIntroduceView.hidden = NO;
        left.hidden = NO;
        right.hidden = YES;
        self.scrollViewLayout.constant = 577+200*self.goodsIntroduceArr.count;
    }
    if (self.selectRightBtn.selected == YES) {
        self.goodsStandardView.hidden = NO;
        self.goodsIntroduceView.hidden = YES;
        left.hidden = YES;
        right.hidden = NO;
        self.scrollViewLayout.constant = 577+200*self.goodsStandardArr.count;
    }
}

//点击查看所有评论的按钮事件
-(IBAction)IntegralCommentAction:(id)sender{
    TCLYCommentViewController * commentViewCtr = [[TCLYCommentViewController alloc] init];
    commentViewCtr.IntegralCommentArr = self.homeModel.goodsCommentArr;
    commentViewCtr.isBaby = YES;
    [self.navigationController pushViewController:commentViewCtr animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//左上方返回的按钮事件
-(void)leftBtnAction{
    if (self.isBabyHome == YES) {
        [self.navigationController popViewControllerAnimated:YES];
        self.navigationController.navigationBarHidden = YES;
    }else{
        [self.navigationController popViewControllerAnimated:YES];
        self.navigationController.navigationBarHidden = NO;
    }
}

//立即抢购的按钮事件
-(void)submitBtnAction{
    TCLYDetermineOrderViewController * submitOrderViewCtr = [[TCLYDetermineOrderViewController alloc] init];
        submitOrderViewCtr.parameterDic = self.parameterDic;
    [self.navigationController pushViewController:submitOrderViewCtr animated:YES];
}

//点击广告也的事件  目前为止没有用
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"%ld",(long)index);
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
